package o;

import com.huawei.hms.framework.common.ContainerUtils;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import com.huawei.hwid.core.constants.HwAccountConstants;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import o.gck.a;
import o.gck.aa;
import o.gck.ab;
import o.gck.ac;
import o.gck.ad;
import o.gck.ae;
import o.gck.af;
import o.gck.ag;
import o.gck.ah;
import o.gck.ai;
import o.gck.ak;
import o.gck.am;
import o.gck.b;
import o.gck.d;
import o.gck.e;
import o.gck.f;
import o.gck.g;
import o.gck.h;
import o.gck.i;
import o.gck.j;
import o.gck.k;
import o.gck.l;
import o.gck.m;
import o.gck.n;
import o.gck.p;
import o.gck.q;
import o.gck.s;
import o.gck.t;
import o.gck.u;
import o.gck.v;
import o.gck.w;
import o.gck.x;
import o.gck.y;
import o.gck.z;
import org.jsoup.select.Selector.SelectorParseException;

public class gcs {
    private static final Pattern ʼ = Pattern.compile("([+-])?(\\d+)");
    private static final String[] ˎ = new String[]{ContainerUtils.KEY_VALUE_DELIMITER, "!=", "^=", "$=", "*=", "~="};
    private static final String[] ॱ = new String[]{",", ">", "+", "~", HwAccountConstants.BLANK};
    private static final Pattern ॱॱ = Pattern.compile("(([+-])?(\\d+)?)n(\\s*([+-])?\\s*\\d+)?", 2);
    private List<gck> ˊ = new ArrayList();
    private gci ˋ;
    private String ˏ;

    private gcs(String str) {
        this.ˏ = str;
        this.ˋ = new gci(str);
    }

    public static gck ˎ(String str) {
        try {
            return new gcs(str).ˋ();
        } catch (IllegalArgumentException e) {
            throw new SelectorParseException(e.getMessage(), new Object[0]);
        }
    }

    gck ˋ() {
        this.ˋ.ˏ();
        if (this.ˋ.ˎ(ॱ)) {
            this.ˊ.add(new j());
            ˎ(this.ˋ.ˋ());
        } else {
            ˊ();
        }
        while (!this.ˋ.ˊ()) {
            boolean ˏ = this.ˋ.ˏ();
            if (this.ˋ.ˎ(ॱ)) {
                ˎ(this.ˋ.ˋ());
            } else if (ˏ) {
                ˎ(' ');
            } else {
                ˊ();
            }
        }
        if (this.ˊ.size() == 1) {
            return (gck) this.ˊ.get(0);
        }
        return new d(this.ˊ);
    }

    private void ˎ(char c) {
        gck o_gck;
        int i;
        gck o_gck2;
        Object obj;
        gck dVar;
        this.ˋ.ˏ();
        gck ˎ = ˎ(ॱ());
        if (this.ˊ.size() == 1) {
            o_gck = (gck) this.ˊ.get(0);
            if (!(o_gck instanceof b) || c == ',') {
                i = 0;
                o_gck2 = o_gck;
            } else {
                i = 1;
                obj = o_gck;
                o_gck = ((b) o_gck).ˊ();
            }
        } else {
            o_gck = new d(this.ˊ);
            i = 0;
            o_gck2 = o_gck;
        }
        this.ˊ.clear();
        if (c == '>') {
            dVar = new d(ˎ, new a(o_gck));
        } else if (c == ' ') {
            dVar = new d(ˎ, new d(o_gck));
        } else if (c == '+') {
            dVar = new d(ˎ, new c(o_gck));
        } else if (c == '~') {
            dVar = new d(ˎ, new g(o_gck));
        } else if (c == ',') {
            if (o_gck instanceof b) {
                o_gck = (b) o_gck;
                o_gck.ˏ(ˎ);
            } else {
                dVar = new b();
                dVar.ˏ(o_gck);
                dVar.ˏ(ˎ);
                o_gck = dVar;
            }
            dVar = o_gck;
        } else {
            throw new SelectorParseException("Unknown combinator: " + c, new Object[0]);
        }
        if (i != 0) {
            ((b) obj).ˋ(dVar);
        } else {
            o_gck2 = dVar;
        }
        this.ˊ.add(obj);
    }

    private String ॱ() {
        StringBuilder ˏ = gbk.ˏ();
        while (!this.ˋ.ˊ()) {
            if (this.ˋ.ॱ("(")) {
                ˏ.append("(").append(this.ˋ.ˊ('(', ')')).append(")");
            } else if (this.ˋ.ॱ("[")) {
                ˏ.append("[").append(this.ˋ.ˊ('[', ']')).append("]");
            } else if (this.ˋ.ˎ(ॱ)) {
                break;
            } else {
                ˏ.append(this.ˋ.ˋ());
            }
        }
        return gbk.ˎ(ˏ);
    }

    private void ˊ() {
        if (this.ˋ.ˋ("#")) {
            ˎ();
        } else if (this.ˋ.ˋ(".")) {
            ˏ();
        } else if (this.ˋ.ॱ() || this.ˋ.ॱ("*|")) {
            ॱॱ();
        } else if (this.ˋ.ॱ("[")) {
            ʼ();
        } else if (this.ˋ.ˋ("*")) {
            ʻ();
        } else if (this.ˋ.ˋ(":lt(")) {
            ʽ();
        } else if (this.ˋ.ˋ(":gt(")) {
            ᐝ();
        } else if (this.ˋ.ˋ(":eq(")) {
            ˊॱ();
        } else if (this.ˋ.ॱ(":has(")) {
            ˋॱ();
        } else if (this.ˋ.ॱ(":contains(")) {
            ˋ(false);
        } else if (this.ˋ.ॱ(":containsOwn(")) {
            ˋ(true);
        } else if (this.ˋ.ॱ(":containsData(")) {
            ॱˊ();
        } else if (this.ˋ.ॱ(":matches(")) {
            ˊ(false);
        } else if (this.ˋ.ॱ(":matchesOwn(")) {
            ˊ(true);
        } else if (this.ˋ.ॱ(":not(")) {
            ˏॱ();
        } else if (this.ˋ.ˋ(":nth-child(")) {
            ˎ(false, false);
        } else if (this.ˋ.ˋ(":nth-last-child(")) {
            ˎ(true, false);
        } else if (this.ˋ.ˋ(":nth-of-type(")) {
            ˎ(false, true);
        } else if (this.ˋ.ˋ(":nth-last-of-type(")) {
            ˎ(true, true);
        } else if (this.ˋ.ˋ(":first-child")) {
            this.ˊ.add(new y());
        } else if (this.ˋ.ˋ(":last-child")) {
            this.ˊ.add(new v());
        } else if (this.ˋ.ˋ(":first-of-type")) {
            this.ˊ.add(new x());
        } else if (this.ˋ.ˋ(":last-of-type")) {
            this.ˊ.add(new u());
        } else if (this.ˋ.ˋ(":only-child")) {
            this.ˊ.add(new ac());
        } else if (this.ˋ.ˋ(":only-of-type")) {
            this.ˊ.add(new ah());
        } else if (this.ˋ.ˋ(":empty")) {
            this.ˊ.add(new w());
        } else if (this.ˋ.ˋ(":root")) {
            this.ˊ.add(new ai());
        } else if (this.ˋ.ˋ(":matchText")) {
            this.ˊ.add(new ag());
        } else {
            throw new SelectorParseException("Could not parse query '%s': unexpected token at '%s'", this.ˏ, this.ˋ.ʻ());
        }
    }

    private void ˎ() {
        String ʽ = this.ˋ.ʽ();
        gbj.ॱ(ʽ);
        this.ˊ.add(new s(ʽ));
    }

    private void ˏ() {
        String ʽ = this.ˋ.ʽ();
        gbj.ॱ(ʽ);
        this.ˊ.add(new k(ʽ.trim()));
    }

    private void ॱॱ() {
        String ᐝ = this.ˋ.ᐝ();
        gbj.ॱ(ᐝ);
        if (ᐝ.startsWith("*|")) {
            this.ˊ.add(new b(new ak(gbg.ˋ(ᐝ)), new am(gbg.ˋ(ᐝ.replace("*|", ":")))));
            return;
        }
        if (ᐝ.contains(HiAnalyticsConstant.REPORT_VAL_SEPARATOR)) {
            ᐝ = ᐝ.replace(HiAnalyticsConstant.REPORT_VAL_SEPARATOR, ":");
        }
        this.ˊ.add(new ak(ᐝ.trim()));
    }

    private void ʼ() {
        gci o_gci = new gci(this.ˋ.ˊ('[', ']'));
        String ˋ = o_gci.ˋ(ˎ);
        gbj.ॱ(ˋ);
        o_gci.ˏ();
        if (o_gci.ˊ()) {
            if (ˋ.startsWith("^")) {
                this.ˊ.add(new b(ˋ.substring(1)));
            } else {
                this.ˊ.add(new d(ˋ));
            }
        } else if (o_gci.ˋ(ContainerUtils.KEY_VALUE_DELIMITER)) {
            this.ˊ.add(new a(ˋ, o_gci.ʻ()));
        } else if (o_gci.ˋ("!=")) {
            this.ˊ.add(new f(ˋ, o_gci.ʻ()));
        } else if (o_gci.ˋ("^=")) {
            this.ˊ.add(new g(ˋ, o_gci.ʻ()));
        } else if (o_gci.ˋ("$=")) {
            this.ˊ.add(new h(ˋ, o_gci.ʻ()));
        } else if (o_gci.ˋ("*=")) {
            this.ˊ.add(new j(ˋ, o_gci.ʻ()));
        } else if (o_gci.ˋ("~=")) {
            this.ˊ.add(new i(ˋ, Pattern.compile(o_gci.ʻ())));
        } else {
            throw new SelectorParseException("Could not parse attribute query '%s': unexpected token at '%s'", this.ˏ, o_gci.ʻ());
        }
    }

    private void ʻ() {
        this.ˊ.add(new e());
    }

    private void ʽ() {
        this.ˊ.add(new p(ͺ()));
    }

    private void ᐝ() {
        this.ˊ.add(new t(ͺ()));
    }

    private void ˊॱ() {
        this.ˊ.add(new q(ͺ()));
    }

    private void ˎ(boolean z, boolean z2) {
        int i = 1;
        int i2 = 0;
        CharSequence ˋ = gbg.ˋ(this.ˋ.ˏ(")"));
        Matcher matcher = ॱॱ.matcher(ˋ);
        Matcher matcher2 = ʼ.matcher(ˋ);
        if ("odd".equals(ˋ)) {
            i2 = 1;
            i = 2;
        } else if ("even".equals(ˋ)) {
            i = 2;
        } else if (matcher.matches()) {
            if (matcher.group(3) != null) {
                i = Integer.parseInt(matcher.group(1).replaceFirst("^\\+", ""));
            }
            if (matcher.group(4) != null) {
                i2 = Integer.parseInt(matcher.group(4).replaceFirst("^\\+", ""));
            }
        } else if (matcher2.matches()) {
            i = 0;
            i2 = Integer.parseInt(matcher2.group().replaceFirst("^\\+", ""));
        } else {
            throw new SelectorParseException("Could not parse nth-index '%s': unexpected format", ˋ);
        }
        if (z2) {
            if (z) {
                this.ˊ.add(new ab(i, i2));
            } else {
                this.ˊ.add(new z(i, i2));
            }
        } else if (z) {
            this.ˊ.add(new ad(i, i2));
        } else {
            this.ˊ.add(new aa(i, i2));
        }
    }

    private int ͺ() {
        String trim = this.ˋ.ˏ(")").trim();
        gbj.ˎ(gbk.ˊ(trim), "Index must be numeric");
        return Integer.parseInt(trim);
    }

    private void ˋॱ() {
        this.ˋ.ˎ(":has");
        String ˊ = this.ˋ.ˊ('(', ')');
        gbj.ˊ(ˊ, ":has(el) subselect must not be empty");
        this.ˊ.add(new b(ˎ(ˊ)));
    }

    private void ˋ(boolean z) {
        this.ˋ.ˎ(z ? ":containsOwn" : ":contains");
        String ʻ = gci.ʻ(this.ˋ.ˊ('(', ')'));
        gbj.ˊ(ʻ, ":contains(text) query must not be empty");
        if (z) {
            this.ˊ.add(new l(ʻ));
        } else {
            this.ˊ.add(new m(ʻ));
        }
    }

    private void ॱˊ() {
        this.ˋ.ˎ(":containsData");
        String ʻ = gci.ʻ(this.ˋ.ˊ('(', ')'));
        gbj.ˊ(ʻ, ":containsData(text) query must not be empty");
        this.ˊ.add(new n(ʻ));
    }

    private void ˊ(boolean z) {
        this.ˋ.ˎ(z ? ":matchesOwn" : ":matches");
        String ˊ = this.ˋ.ˊ('(', ')');
        gbj.ˊ(ˊ, ":matches(regex) query must not be empty");
        if (z) {
            this.ˊ.add(new af(Pattern.compile(ˊ)));
        } else {
            this.ˊ.add(new ae(Pattern.compile(ˊ)));
        }
    }

    private void ˏॱ() {
        this.ˋ.ˎ(":not");
        String ˊ = this.ˋ.ˊ('(', ')');
        gbj.ˊ(ˊ, ":not(selector) subselect must not be empty");
        this.ˊ.add(new e(ˎ(ˊ)));
    }
}
